// carPackage/pages/feedBack/feedBack.js


import { chooseMultipleOSSImgUploadQiniuAction, chooseAndUploadOSSActicon } from '../../../service/upload_service'
import {
  createCustomNeed
} from "../../../service/jb_common_service"
import {
  getFrontUploadSign
} from "../../../service/config"
import debounce from "../../../tools/debounce"

let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    showSuccessPop: false,
    feedArrs: [
      {
        title: "补水保湿",
        value: 1,
        isChoose: false,
      },
      {
        title: "术后修复",
        value: 2,
        isChoose: false,
      },
      {
        title: "控油祛痘",
        value: 3,
        isChoose: false,
      },
      {
        title: "冻龄抗衰",
        value: 4,
        isChoose: false,
      },
      {
        title: "舒敏修复",
        value: 5,
        isChoose: false,
      },
      {
        title: "美白淡斑",
        value: 6,
        isChoose: false,
      },
      {
        title: "其他问题",
        value: 7,
        isChoose: false,
      },

    ],
    feedIndex: 10,
    carDescNum: 0,
    formData: {
      problem: "",
      type: "",
      images: [],
    },
    uploadData: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.configQues();
    this.getFrontUploadSign();
  },
  configQues() {
    if (app.globalData.displayArr && app.globalData.displayArr.length > 0) {
      // console.log("JJLog:dispArr:" + JSON.stringify(app.globalData.displayArr));
      let arr = [];
      app.globalData.displayArr.forEach(i => {
        arr.push({
          title: i.name,
          isChoose: false,
        })
      })
      arr.push({ title: "其他问题", isChoose: false })
      this.setData({
        feedArrs: arr
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },
  feedItemAction(e) {
    let item = e.currentTarget.dataset.item;
    let index = e.currentTarget.dataset.index;
    item.isChoose = !item.isChoose
    let cItem = 'feedArrs[' + index + ']';
    this.setData({
      [cItem]: item,
      feedIndex: index,
    })
    // this.setData({
    //   feedIndex: index,
    //   // 'formData.type': item.title
    // })

  },
  carDescInput(e) {
    this.setData({
      'formData.problem': e.detail.value,
      carDescNum: e.detail.cursor
    })
  },
  //提交数据
  submitInfo() {

    let types = this.data.feedArrs.filter(item => item.isChoose).map(item => item.title)
    if (types.length > 0) {
      this.setData({
        'formData.type': types.join(',')
      })
    } else {
      this.setData({
        'formData.type': ''
      })
    }

    if (!this.data.formData.type) {
      wx.showToast({
        title: '请选择定制类型',
        icon: "none"
      })
      return;
    }

    if (!this.data.formData.problem) {
      wx.showToast({
        title: '请输入您的问题描述',
        icon: "none"
      })
      return;
    }
    console.log("JJLog:提交内容是哈哈哈哈：" + JSON.stringify(this.data.formData));

    debounce(this.submitAction);

  },
  submitAction() {
    wx.showLoading();
    createCustomNeed(this.data.formData).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        this.setData({
          showSuccessPop: true
        })
        app.globalData.isCusSubmit = true;
      }
    }).catch(err => {
      console.error("createCustomNeed:", err);
      wx.hideLoading();
      wx.showToast({
        title: err || "提交失败",
        icon: "none"
      })
    })
  },

  successPopHide() {
    this.setData({
      showSuccessPop: false
    })
  },
  sureAction() {
    this.setData({
      showSuccessPop: false
    })
    wx.navigateBack({
      delta: 0,
    })
  },
  pushMultiImgs() {
    let arr = [];
    arr = this.data.formData.images || [];
    let num = 6;
    if (arr.length == 6) {
      wx.showToast({
        title: '最多上传6张图片',
        icon: "none"
      })
      return;
    }
    num = 6 - arr.length;
    chooseMultipleOSSImgUploadQiniuAction('custom', num, this.data.uploadData).then(res => {
      console.log("JJLog:图片上传成功:" + JSON.stringify(res));
      wx.showToast({
        title: '上传成功',
      })
      if (res && res.length > 0) {
        res.forEach(i => {
          arr.push(i.imgUrl)
        })
        this.setData({
          'formData.images': arr
        })
      }


    }).catch(err => {
      console.log("JJLog:图片上传失败:" + JSON.stringify(err));
      wx.showToast({
        title: '图片上传失败',
        icon: "none"
      })
    })
  },
  getFrontUploadSign() {
    getFrontUploadSign().then(res => {
      console.log("JJLog:获取到的图片签名是:" + JSON.stringify(res));
      if (res.code == 200) {
        wx.setStorageSync('ossHost', res.data.host);
        wx.setStorageSync('ossSignature', res.data.signature);
        wx.setStorageSync('ossPolicy', res.data.policy);
        wx.setStorageSync('ossAccessId', res.data.accessId);
        wx.setStorageSync('ossCallBack', res.data.callback);
        wx.setStorageSync('ossDir', res.data.dir);

        this.setData({
          uploadData: res.data
        })
      }
    }).catch(err => {
      console.error("JJLog:getFrontUploadSign", err);
    })
  },
  cancelPushImgAction(e) {
    let index = e.currentTarget.dataset.index;
    let arr = this.data.formData.images;
    arr.splice(index, 1);
    this.setData({
      'formData.images': arr
    })
  },






})